//
//  ViewController.m
//  08-HomeWork
//
//  Created by GivenBmli on 16/7/25.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
#import "PrefixHeader.pch"
#import "Models.h"
#import "DetailsView.h"

@interface ViewController ()<UITableViewDelegate , UITableViewDataSource>
@property (nonatomic ,retain)UITableView *tableView;
@property (nonatomic ,retain)NSMutableArray *array;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];
    self.array = [[Models groups] mutableCopy];;
    
    self.navigationItem.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    [self creatTableView];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

- (NSArray <UITableViewRowAction *>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [_array removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UILayoutPriorityDefaultLow];
    }];
    delete.backgroundColor =[UIColor redColor];
    return @[delete];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDETH, HEIGHT) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor colorWithRed:0.502 green:0.000 blue:0.251 alpha:1.000];
    [self.view addSubview:_tableView];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:ID];
    }
    cell.textLabel.text = [[_array objectAtIndex:indexPath.row] name];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    _tableView.separatorStyle = 0;
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailsView *detail = [[DetailsView alloc] init];
    Models *model = self.array[indexPath.row];
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];
}

 


@end
